package net.huashitong.scheduling.web;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.scheduling.entity.TownshipCompletion;
import net.huashitong.scheduling.entity.TownshipGrade;
import net.huashitong.scheduling.filter.AreaRankingFilter;
import net.huashitong.scheduling.filter.TownshipCompletionFiter;
import net.huashitong.scheduling.service.TownshipCompletionService;
import net.huashitong.scheduling.service.TownshipGradeService;
@Controller
@RequestMapping("/app/townshipCompletion/")
public class TownshipCompletionController extends JsonBaseController{
	@Autowired
	private TownshipCompletionService townshipCompletionService;
	@Autowired
	private TownshipGradeService townshipGradeService;
	
	 @Override
	 protected String getView(String arg0) {
			return null;
	} 
	 public static final String[] months = {"12","11","10","09","08","07","06","05","04","03","02","01"};
	 public static final String[] quas = {"12","09","06","03"};
	public static final String[] years={"2022", "2021","2020","2019","2018","2017","2016"};
	 @ResponseBody
	 @RequestMapping("/queryIndustryName")
	 public Message queryIndustryName(@RequestBody TownshipCompletionFiter filter){
		 List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		 List<TownshipCompletion>  tcList=townshipCompletionService.queryIndustryName();
		 for(TownshipCompletion tc:tcList){
			 Map<String, Object> map = new HashMap<String, Object>();
			 map.put("industryId", tc.getIndustryId());
			 map.put("industryName",tc.getIndustryName());
			 lists.add(map);
		 }
	    return coverMessage("200","数据获取成功",lists);
	 }
	 @ResponseBody
	 @RequestMapping("/queryTownshipCompletionList")
	 public Message queryTownshipCompletionList(@RequestBody TownshipCompletionFiter filter){
		 Map<String, Object> map = new HashMap<String, Object>();
		 List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		 List<TownshipCompletion> tcList=new ArrayList<TownshipCompletion>();
		 DecimalFormat df=new DecimalFormat("#.##");
		 if(ObjectUtils.isBlank(filter.getDate())){
			 for(int j=0;j<TownshipCompletionController.years.length;j++){
				 for(int i=0; i<TownshipCompletionController.months.length;i++){
					 filter.setDate(years[j]+months[i]);
					 tcList=townshipCompletionService.queryTownshipCompletionList(filter);
					 if(ObjectUtils.isNotBlank(tcList)){
						 break;
					 }
				 }
			 }
		 }else{
			 tcList=townshipCompletionService.queryTownshipCompletionList(filter);
		 }
		 int s=1;
		 if(ObjectUtils.isNotBlank(tcList)){
			 for(TownshipCompletion tc:tcList){
				 if("蕉城区".equals(tc.getArea())){
					 map.put("area", tc.getArea());
					 map.put("cumulative", df.format(Double.parseDouble(tc.getCumulative())));
					 map.put("ratio", df.format(Double.parseDouble(tc.getRatio())));
					 String month=tc.getCurrentMonth().substring(4,6);
			  		 String year=tc.getCurrentMonth().substring(0,4);
					 map.put("date",year+"年"+month+"月");
					 map.put("unit", tc.getUnit());
				 }else{
					 Map<String, Object> maps = new HashMap<String, Object>();
					 maps.put("area", tc.getArea());
					 maps.put("cumulative", df.format(Double.parseDouble(tc.getCumulative())));
					 maps.put("ratio", df.format(Double.parseDouble(tc.getRatio())));
					 maps.put("date", tc.getCurrentMonth());
					 maps.put("unit", tc.getUnit());
					 maps.put("state", filter.getState());
					 if("0".equals(filter.getState())){
						 maps.put("cumulativeRanking",s);
					 }else if("1".equals(filter.getState())){
						 maps.put("ratioRanking",s);
					 }
					
					s++;
					 lists.add(maps);
				 }
			 }
		 }
		 map.put("data", lists);
		 map.put("state", filter.getState());
		 return coverMessage("200","数据获取成功",map);
	 }
	 @ResponseBody
	 @RequestMapping("/queryTownshipGradeList")
	 public Message  queryTownshipGradeList(@RequestBody TownshipCompletionFiter filter){
		 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		 List<TownshipGrade> lists=new ArrayList<TownshipGrade>();
		  Map<String, Object> map = new HashMap<String, Object>();
		 String date="";
		 if(ObjectUtils.isBlank(filter.getDate())){
			 for(int j=0;j<TownshipCompletionController.years.length;j++){
				 for(int i=0; i<TownshipCompletionController.quas.length;i++){
					  lists =townshipGradeService.queryByMonth(years[j]+quas[i],"0");
					  date=years[j]+quas[i];
					 if(ObjectUtils.isNotBlank(lists)){
						 break;
					 }
				 }
			 }
		 }else{
			 lists =townshipGradeService.queryByMonth(filter.getDate(),"0");
			 String month=filter.getDate().substring(4,6);
	  		 String year=filter.getDate().substring(0,4);
	  		 date=filter.getDate();
		 }
		 if(ObjectUtils.isNotBlank(lists)){
			 for(TownshipGrade tg:lists){
		     Map<String, Object> maps = new HashMap<String, Object>();
			 maps.put("sysId", tg.getSysId());
			 maps.put("area", tg.getArea());
			 maps.put("totalPoints",tg.getTotalPoints());
			 maps.put("reatioRanking", tg.getReatioRanking());
			 list.add(maps);
			 }
		 }
		 map.put("date",date);
		 map.put("lists", list);
		 return coverMessage("200","数据获取成功",map);
	 }
	 @ResponseBody
	 @RequestMapping("/queryTownshipGradeDetail")
	 public  Message  queryTownshipGradeDetail(@RequestBody TownshipCompletionFiter filter){
		 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		 List<TownshipGrade> lists=townshipGradeService.queryByParentId(filter.getSysId());
		 DecimalFormat df=new DecimalFormat("##.##");
		 if(ObjectUtils.isNotBlank(lists)){
			 for(TownshipGrade tg:lists){
			     Map<String, Object> maps = new HashMap<String, Object>();
				 maps.put("sysId", tg.getSysId());
				 maps.put("area", tg.getArea()); //地区 
				 maps.put("industryName",tg.getIndustryName()); //指标名称
				 maps.put("totalPoints",tg.getTotalPoints());   //总得分
				 maps.put("reatioRanking", tg.getReatioRanking());//排名
				if("-".equals(tg.getCumulative())||"持平".equals(tg.getCumulative())){
					 maps.put("cumulative", tg.getCumulative());//累计数
				}else{
					 maps.put("cumulative", df.format(Double.parseDouble(tg.getCumulative())));//累计数
				}
				if("-".equals(tg.getRatio())||"持平".equals(tg.getRatio())){
					 maps.put("ratio", tg.getRatio());//增长率
				}else{
					 maps.put("ratio", df.format(Double.parseDouble(tg.getRatio())));//增长率
				}
				if("-".equals(tg.getContributionRatio())||"持平".equals(tg.getContributionRatio())){
					 maps.put("contributionRatio", tg.getContributionRatio());//贡献率
				}else{
					 maps.put("contributionRatio", df.format(Double.parseDouble(tg.getContributionRatio())));//贡献率
				}
				 maps.put("reatioPoints", tg.getReatioPoints());//增长率得分
				 maps.put("contributionPoints", tg.getContributionPoints());//贡献率得分
				 maps.put("unit", tg.getUnit());
				 list.add(maps);
			 }
		 }
		 return coverMessage("200","数据获取成功",list);
	 }
}
